package com.shoujifeng.snowmusic.player;

import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.player.http.ServerAccess;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class UnknowPasswordActivity extends Activity {
	private Button breakBtn;
	private Button getCheckingBtn;
	private EditText numberEdt;
	private ServerAccess serverAccess;
	private Thread accThread;
	private final int UPDATE_DATA = 0;
	private final int UPDATE_ONE = 1;
	private String code;
	private String code_type;
	private EditText passwordEdt;
	private EditText verifyPasswordEdt;
	private Button registerBtn;
	private EditText authCode;
	private String authOrRegister = "";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.unknow_password);
		init();
	}

	private void init() {
		serverAccess = new ServerAccess(UnknowPasswordActivity.this);
		breakBtn = (Button) findViewById(R.id.breakBtn);
		breakBtn.setOnClickListener(onClickListener);
		numberEdt = (EditText) findViewById(R.id.numberEdt);
		getCheckingBtn = (Button) findViewById(R.id.getCheckingBtn);
		getCheckingBtn.setOnClickListener(onClickListener);
		passwordEdt = (EditText) findViewById(R.id.passwordEdt);
		verifyPasswordEdt = (EditText) findViewById(R.id.verifyPasswordEdt);
		registerBtn = (Button) findViewById(R.id.registerBtn);
		registerBtn.setOnClickListener(onClickListener);
		authCode = (EditText) findViewById(R.id.authCode);
	}

	private View.OnClickListener onClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.breakBtn: {
				finish();
			}
				break;
			case R.id.getCheckingBtn: {
				if (numberEdt.equals("")) {
					Toast.makeText(UnknowPasswordActivity.this, "请先输入手机码号!",
							Toast.LENGTH_LONG).show();
				} else {
					authOrRegister = "auth";
					Toast.makeText(UnknowPasswordActivity.this, "正在发送!",
							Toast.LENGTH_LONG).show();
					accThread = new Thread(runnable);
					accThread.start();
				}

			}
				break;
			case R.id.registerBtn: {
				if (passwordEdt.getText().toString().length() < 6) {
					Toast.makeText(UnknowPasswordActivity.this, "密码不能少于6位数!",
							Toast.LENGTH_SHORT).show();
				} else if (!verifyPasswordEdt.getText().toString()
						.equals(passwordEdt.getText().toString())) {
					Toast.makeText(UnknowPasswordActivity.this, "两次输入的密码不一致!",
							Toast.LENGTH_SHORT).show();
				} else if (authCode.getText().toString().equals("")) {
					Toast.makeText(UnknowPasswordActivity.this, "验证码不能为空!",
							Toast.LENGTH_SHORT).show();
				} else if (!authCode.getText().toString().equals(code)) {
					Toast.makeText(UnknowPasswordActivity.this, "验证码不正确!",
							Toast.LENGTH_SHORT).show();
				} else {
					authOrRegister = "register";
					accThread = new Thread(runnable);
					accThread.start();
				}

			}
			default:
				break;
			}
		}
	};

	protected Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case UPDATE_ONE: {
				if (code_type.equals("0")) {
					Toast.makeText(UnknowPasswordActivity.this, "修改失败，请重试!",
							Toast.LENGTH_SHORT).show();
				}
				if (code_type.equals("1")) {
					Toast.makeText(UnknowPasswordActivity.this, "修改成功!",
							Toast.LENGTH_SHORT).show();
					Intent intent = new Intent(UnknowPasswordActivity.this,
							LoginActivity.class);
					startActivity(intent);
				}
				if (code_type.equals("2")) {
					Toast.makeText(UnknowPasswordActivity.this, "没有该账号，请重试!",
							Toast.LENGTH_SHORT).show();
				}

			}
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	// 创建多线程获取数据
	protected Runnable runnable = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				if (authOrRegister.equals("auth")) {
					code = serverAccess.getChecking(numberEdt.getText()
							.toString());
					authOrRegister = "";
					sendMessage(UPDATE_DATA);
				}
				if (authOrRegister.equals("register")) {
					code_type = serverAccess.revampPassword(numberEdt.getText()
							.toString(), passwordEdt.getText().toString());
					authOrRegister = "";
					sendMessage(UPDATE_ONE);
				}

			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	};

	// 发送消息到Handler
	private void sendMessage(int what) {
		Message msg = new Message();
		msg.what = what;
		handler.sendMessage(msg);
	}
}
